package com.qingke.easyjava.jdbc.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.easyjava.jdbc.command.impl.AcceptCommand;
import com.qingke.easyjava.jdbc.command.impl.AnswerCommand;
import com.qingke.easyjava.jdbc.command.impl.AskCommand;
import com.qingke.easyjava.jdbc.command.impl.ListCommand;
import com.qingke.easyjava.jdbc.command.impl.ScoreCommand;
import com.qingke.easyjava.jdbc.pojo.Player;

public class PlayerCommandFactory extends SystemCommandFactory {

    private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

    static {
        commandMap.put(CommandCode.LIST, ListCommand.class);
        commandMap.put(CommandCode.SCORE, ScoreCommand.class);
        commandMap.put(CommandCode.ASK, AskCommand.class);
        commandMap.put(CommandCode.ANSWER, AnswerCommand.class);
        commandMap.put(CommandCode.ACCEPT, AcceptCommand.class);
    }

    private Player player;

    public PlayerCommandFactory(Player player) {
        this.player = player;
    }

    @Override
    public Command buildCommand(CommandCode cmdCode) {
        Command command = null;

        Class<? extends Command> cmdClass = commandMap.get(cmdCode);
        
        if (cmdClass != null) {
            try {
                command = cmdClass.getConstructor(Player.class).newInstance(player);
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e)
            {
                e.printStackTrace();
            }
        }
        
        if (command == null) {
            command = super.buildCommand(cmdCode);
        }
        
        return command;
    }

}
